(function () {

	'use strict';

	angular
		.module('app')
		.controller('MyOrderController', MyOrderController);

	MyOrderController.$inject = ['$scope', '$http', '$window', '$timeout', '$location', '$state', '$stateParams', 'API', 'ENUM', 'PaymentModel', 'MyOrderModel', 'OrderExpressModel'];

	function MyOrderController($scope, $http, $window, $timeout, $location, $state, $stateParams, API, ENUM, PaymentModel, MyOrderModel, OrderExpressModel) {

		$scope.TAB_ALL = 0;
		$scope.TAB_CREATED = 1;
		$scope.TAB_PAID = 3;
		$scope.TAB_DELIVERING = 4;
		$scope.TAB_DELIVERIED = 5;
		$scope.currentTab = $scope.TAB_ALL;
		$scope.cancellingOrder = null;
		$scope.showDialog = false;
		$scope.myOrderModel = MyOrderModel;
		$scope.paymentModel = PaymentModel;
		$scope.orderExpressModel = OrderExpressModel;

		if ($stateParams.tab == 'all') {
			$scope.currentTab = $scope.TAB_ALL;
			$scope.myOrderModel.status = null;
		} else if ($stateParams.tab == 'created') {
			$scope.currentTab = $scope.TAB_CREATED;
			$scope.myOrderModel.status = 1;
		} else if ($stateParams.tab == 'paid') {
			$scope.currentTab = $scope.TAB_PAID;
			$scope.myOrderModel.status = 3;
		} else if ($stateParams.tab == 'delivering') {
			$scope.currentTab = $scope.TAB_DELIVERING;
			$scope.myOrderModel.status = 4;
		} else if ($stateParams.tab == 'delivered') {
			$scope.currentTab = $scope.TAB_DELIVERIED;
			$scope.myOrderModel.status = 5;
		} else {
			$scope.currentTab = $scope.TAB_ALL;
			$scope.myOrderModel.status = null;
		}

		$scope.touchTabAll = function () {
            if ($scope.currentTab != $scope.TAB_ALL) {
                $scope.currentTab = $scope.TAB_ALL;
                $scope.myOrderModel.status = null;
                $scope.myOrderModel.reload();
            }
        };

		$scope.touchTabCreated = function () {
            if ($scope.currentTab != $scope.TAB_CREATED) {
                $scope.currentTab = $scope.TAB_CREATED;
                $scope.myOrderModel.status = 1;
                $scope.myOrderModel.reload();
            }
        };

		$scope.touchTabPaid = function () {
            if ($scope.currentTab != $scope.TAB_PAID) {
                $scope.currentTab = $scope.TAB_PAID;
                $scope.myOrderModel.status = 3;
                $scope.myOrderModel.reload();
            }
        };

		$scope.touchTabDelivering = function () {
            if ($scope.currentTab != $scope.TAB_DELIVERING) {
                $scope.currentTab = $scope.TAB_DELIVERING;
                $scope.myOrderModel.status = 4;
                $scope.myOrderModel.reload();
            }
        };

		$scope.touchTabDeliveried = function () {
            if ($scope.currentTab != $scope.TAB_DELIVERIED) {
                $scope.currentTab = $scope.TAB_DELIVERIED;
                $scope.myOrderModel.status = 5;
                $scope.myOrderModel.reload();
            }
        };

		$scope.touchDialogCancel = function () {
            $scope.cancellingOrder = null;
            $scope.showDialog = false;
        };

		$scope.touchDialogConfirm = function () {
            API.order.cancel({
                order_id: $scope.cancellingOrder.order_id
            }).then(function (res) {
                $scope.myOrderModel.reload();
                $scope.cancellingOrder = null;
                $scope.showDialog = false;
            });
        };

		$scope.touchOrder = function (order_id) {
            $state.go('order-detail', {
                order_id: order_id
            });
        };

		$scope.touchPay = function (order) {
            if (order) {
                $scope.paymentModel.clear();
                $state.go('payment', {
                    order_id: order.order_id
                });
            }
        };

		$scope.touchCancel = function (order) {
            $scope.cancellingOrder = order;
            $scope.showDialog = true;
        };

		$scope.touchConfirm = function (order_id) {
            API.order.confirmReceipt({
                order_id: order_id,
            }).then(function (order) {
                $state.go('confirm-delivery', {
                    order_id: order.order_id
                });
            });
        };

		$scope.touchExpress = function (order) {
            $scope.orderExpressModel.clear();
            $scope.orderExpressModel.order = order;

            $state.go('order-express', {
                order_id: order.order_id
            });
        };

		$scope.touchComment = function (order_id) {
            $state.go('order-review', {
                order_id: order_id
            });
        };

		$scope.myOrderModel.reload();
	}

})();